package org.zjt.thread;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Method {

    public static void main(String[] args) throws IOException {
        Integer i = new Integer(1);
        System.out.println(main2(i));
       Optional.of(1).ifPresent(System.out::println);
        Logger global = Logger.getGlobal();
        //global.config(() -> "22222222");
        global.setLevel(Level.ALL);
        System.out.println(global.getLevel());
        global.info("---------------------------");

        Arrays.stream(Method.class.getDeclaredMethods()).map(item -> item.getName()).forEach(System.out::println);

        compiler();

    }

    public static void compiler() throws IOException {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int result = compiler.run(null, null, null, "/home/zhang/IdeaProjects/learning/java-basic/src/main/java/org/zjt/thread/Method.java");
        System.out.println(result == 0 ? "编译成功" : "编译失败");
        //执行java 命令 , 空参数, 所在文件夹
        Process process = Runtime.getRuntime().exec("java Method",null,new File("/home/zhang/IdeaProjects/learning/java-basic/src/main/java/org/zjt/thread"));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            System.out.println(str);
        }
    }

    public static Integer main2(Integer arg) {
        try {
            arg = 1 / 0;
        }catch (Exception e) {
            arg = 3;
        } finally{
            arg = 2;
        }
        return arg;
    }
}
